package mgodb

import (
	"github.com/facebookgo/inject"
	"github.com/globalsign/mgo"
	"github.com/globalsign/mgo/bson"
	"github.com/pkg/errors"
	"jgt-oper/src/model"
	"jgt-oper/src/schema"
	"jgt-oper/src/service/mongo"
	"time"
)

// cEnterprisePark 企业门牌数据表
const cEnterprisePark = "oper_enterprise_park"

// EnterprisePark 企业管理
type EnterprisePark struct {
	DB *mongo.DB
}

// Init 初始化
func (a *EnterprisePark) Init(g *inject.Graph, db *mongo.DB) {
	a.DB = db

	// 初始化mongo索引
	a.DB.C(cEnterprisePark, func(c *mgo.Collection) error {
		c.EnsureIndex(mgo.Index{
			Key:    []string{"enterprise_id", "deleted"},
			Unique: true,
		})
		c.EnsureIndexKey("enterprise_id")
		return nil
	})

	var val model.IEnterprisePark = a
	g.Provide(&inject.Object{Value: val, Name: "IEnterprisePark"})
}

// Create 创建企业-门牌 对应关系
func (a *EnterprisePark) Create(item *schema.EnterprisePark) error {
	var (
		err error
	)
	//创建新的条目
	err = a.DB.C(cEnterprisePark, func(c *mgo.Collection) error {
		id, err := a.DB.IncrID(cEnterprisePark)
		if err != nil {
			return err
		}
		item.ID = id
		return c.Insert(item)
	})
	if err != nil {
		return errors.Wrapf(err, "创建企业-门牌对应关系出错")
	}
	return nil
}

// Get 获取企业-门牌 对应关系
func (a *EnterprisePark) Get(enterpriseID string) (*schema.EnterprisePark, error) {
	var (
		item *schema.EnterprisePark
		err  error
	)
	item = new(schema.EnterprisePark)
	err = a.DB.C(cEnterprisePark, func(c *mgo.Collection) error {
		return c.Find(bson.M{"enterprise_id": enterpriseID, "deleted": 0}).One(item)
	})
	if err != nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, errors.Wrapf(err, "获取企业-门牌对应关系出错")
	}
	return item, nil
}

// Delete 标记删除企业门牌对应关系条目
func (a *EnterprisePark) Delete(enterpriseID string) error {
	err := a.DB.C(cEnterprisePark, func(c *mgo.Collection) error {
		return c.Update(bson.M{"enterprise_id": enterpriseID, "deleted": 0}, bson.M{"$set": bson.M{"deleted": time.Now().Unix()}})
	})
	if err != nil {
		return errors.Wrapf(err, "删除企业-门牌对应关系出错")
	}
	return nil
}

// AddNodes 增加企业门牌
func (a *EnterprisePark) AddNodes(enterpriseID string, nodes []string) error {
	var (
		err error
	)
	err = a.DB.C(cEnterprisePark, func(c *mgo.Collection) error {
		return c.Update(bson.M{"enterprise_id": enterpriseID, "deleted": 0},
			bson.M{"$addToSet": bson.M{"nodes": bson.M{"$each": nodes}}})
	})
	if err != nil {
		return errors.Wrapf(err, "增加企业门牌出错")
	}
	return nil
}

// DeleteNodes 减少企业门牌
func (a *EnterprisePark) DeleteNodes(enterpriseID string, nodes []string) error {
	var (
		err error
	)
	err = a.DB.C(cEnterprisePark, func(c *mgo.Collection) error {
		return c.Update(bson.M{"enterprise_id": enterpriseID, "deleted": 0},
			bson.M{"$pull": bson.M{"nodes": bson.M{"$in": nodes}}})
	})
	if err != nil {
		return errors.Wrapf(err, "删除企业门牌出错")
	}
	return nil
}

// FreezeEntrance 冻结门禁设备
func (a *EnterprisePark) FreezeEntrance(param *schema.FreezeParams) error {

	var (
		err error
	)
	err = a.DB.C(cEnterprisePark, func(c *mgo.Collection) error {
		return c.Update(bson.M{"enterprise_id": param.EnterpriseID, "deleted": 0},
			bson.M{"$addToSet": bson.M{"entrances_frozen": param.EntranceID}})
	})
	if err != nil {
		return errors.Wrapf(err, "增加冻结门禁错误")
	}
	return nil
}

// UnfreezeEntrance 解冻门禁设备
func (a *EnterprisePark) UnfreezeEntrance(param *schema.FreezeParams) error {
	var (
		err error
	)
	err = a.DB.C(cEnterprisePark, func(c *mgo.Collection) error {
		return c.Update(bson.M{"enterprise_id": param.EnterpriseID, "deleted": 0},
			bson.M{"$pull": bson.M{"entrances_frozen": param.EntranceID}})
	})
	if err != nil {
		return errors.Wrapf(err, "减少冻结门禁错误")
	}
	return nil
}
